//
//  BankCardView.m
//  SZBank
//
//  Created by rimi on 16/7/21.
//  Copyright © 2016年 LuoYao. All rights reserved.
//

#import "BankCardView.h"
#import "MJRefresh.h"
#import "BankCardPopupView.h"
#define IMAGEURL @"http://117.172.47.171:1113/szbank"
@interface BankCardView()<ButtonAndTableViewDelegate>
{
    CGFloat frameWidth;/**  自身宽度 */
    CGFloat frameHeight;/**  自身高度 */
    NSMutableDictionary *dataDictionary;
}
@property (nonatomic,strong) UIImageView *bgImageView;/**  背景图 */

@property (nonatomic,strong) UIScrollView *scrollView;/**  滚动视图 */

@property(nonatomic) BankCardPopupView *popupView;
@end
@implementation BankCardView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        frameWidth = self.frame.size.width;
        frameHeight = self.frame.size.height;
        [self addSubview:self.bgImageView];
        UIImageView *imageLine = [[UIImageView alloc] initWithFrame:CGRectMake(0, frameHeight * 0.96, frameWidth, frameHeight * 0.05)];
        imageLine.image = [LocalDataManager getImageWithSourceOfPath:@"金融资讯_底边"];
        [self addSubview:imageLine];
        /**  下拉按钮 */
        NSDictionary *dic = @{@"title":@"借记卡",@"list":@[@"借记卡",@"贷记卡"]};
        ButtonAndTableView *btView = [[ButtonAndTableView alloc] initWithFrame:CGRectMake(frameWidth * 0.02, frameHeight * 0.01, frameWidth * 0.96, frameHeight * 0.07) dataSource:dic backImageName:@"储蓄存款_01" selectImageName:@"储蓄存款_01"];
        btView.delegate = self;
        [self addSubview:btView];

        dataDictionary = [NSMutableDictionary dictionary];
        NSMutableArray *imageArray = [NSMutableArray array];/**  图片 */
        NSMutableArray *nameArray = [NSMutableArray array];/**  银行卡名 */
        NSMutableArray *featureArray = [NSMutableArray array];/**  特色 */
         NSMutableArray *conditionArray = [NSMutableArray array];/**  条件 */
        NSMutableArray *noticeArray = [NSMutableArray array];/**  背景图 */
        /**  银行卡数据请求 */
        [NetWorkingDataManager getProtocolTemplateUpdateWithProtocolid:@"402880c43fe174f9013fe17c1e990001" success:^(id sender){
            NSInteger index = 0;
            for (NSDictionary *dic1 in sender) {
                if (index == 0 || index == 3 || index == 4) {
                    NSDictionary *dic2 = [dic1 objectForKey:@"protocolData"];
                    if (index == 0) {
                        [imageArray addObject:@"透明"];
                    }else{
                        for (NSString *str in [dic2 objectForKey:@"图片"]) {
                            [imageArray addObject:str];
                        }
                    }
                    [nameArray addObject:[dic2 objectForKey:@"银行卡名称"][0]];
                    [featureArray addObject:[dic2 objectForKey:@"产品特色"][0]];
                    [conditionArray addObject:[dic2 objectForKey:@"申办条件"][0]];
                    [noticeArray addObject:[dic2 objectForKey:@"申请人须知"][0]];
                    
                }
                index ++;
            }
            [dataDictionary setObject:imageArray forKey:@"图片"];
            [dataDictionary setObject:nameArray forKey:@"银行卡名称"];
            [dataDictionary setObject:featureArray forKey:@"产品特色"];
            [dataDictionary setObject:conditionArray forKey:@"申办条件"];
            [dataDictionary setObject:noticeArray forKey:@"申请人须知"];
            
            [self initUserInterface];
        } failure:nil];
        
        
        
    }
    return self;
}

- (void)initUserInterface{
    self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(40, frameHeight * 0.1, frameWidth * 0.95, frameHeight * 0.6)];
    self.scrollView.contentSize = CGSizeMake(frameWidth * 0.95, frameHeight * 0.3);
    self.scrollView.backgroundColor = [UIColor clearColor];
    [self addSubview:self.scrollView];
    /**  下拉刷新 */
    self.scrollView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self.scrollView.mj_header endRefreshing];
    }];
    for (int i = 0; i < 3; i ++){
        /**  图片按钮 */
        UIButton *cardButton = [[UIButton alloc] initWithFrame:CGRectMake(i * frameWidth * 0.32, 0, frameWidth * 0.3, frameHeight * 0.3)];
        [cardButton setBackgroundImage:[LocalDataManager getImageWithSourceOfPath:@"银行业务_小背景"] forState:UIControlStateNormal];
        [cardButton addTarget:self action:@selector(action_cardButton:) forControlEvents:UIControlEventTouchUpInside];
        cardButton.tag = 1000 + i;
        /**  银行卡图片 */
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(i * frameWidth * 0.32 + 50, frameHeight * 0.025, frameWidth * 0.2, frameHeight * 0.2)];
        if (i != 0){
            NSString *imageString = [NSString stringWithFormat:@"%@%@",IMAGEURL,[dataDictionary objectForKey:@"图片"][i]];
            UIImage *imageURL = [LocalDataManager getImageWithURL:imageString];
            imageView.image = imageURL;
        }
        /**  银行卡名 */
        NSString *titleString = [NSString stringWithFormat:@"%@%@%@",@"【",[dataDictionary objectForKey:@"银行卡名称"][i],@"】"];
        UILabel *cardName = [[UILabel alloc] initWithFrame:CGRectMake(i * frameWidth * 0.32 + 100, frameHeight * 0.23, frameWidth * 0.15, frameHeight * 0.03)];
        cardName.text = titleString;
        
        [self.scrollView addSubview:cardButton];
        [self.scrollView addSubview:cardName];
        [self.scrollView addSubview:imageView];
    }
}

- (void)action_cardButton:(UIButton *)sender{
    [[UIApplication sharedApplication].keyWindow.rootViewController.view addSubview:self.popupView];
    self.popupView.index = sender.tag - 1000;
    self.popupView.sourceDic = dataDictionary;
    [self.popupView initUserInterface];
    self.popupView.alpha = 0;
    /**  弹出动画 */
    [UIView animateWithDuration:0.5 animations:^{
        self.popupView.alpha = 1;
    } completion:nil];
}

- (void)btnTableView:(ButtonAndTableView *)btnTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath selectTitle:(NSString *)selectTitle{
    if ([selectTitle isEqualToString:@"贷记卡"]) {
        [self.scrollView removeFromSuperview];
    }else{
        [self addSubview:self.scrollView];
    }
}

- (UIImageView *)bgImageView{
    if (!_bgImageView) {
        _bgImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, frameWidth, frameHeight)];
        _bgImageView.image = [LocalDataManager getImageWithSourceOfPath:@"内容框背景"];
    }
    return _bgImageView;
}

- (BankCardPopupView *)popupView{
    if (!_popupView) {
        _popupView = [[BankCardPopupView alloc] initWithFrame:CGRectMake(0, 0, KWIDTH, KHEIGHT)];
    }
    return _popupView;
}
@end
